<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Cart extends Frontend {

    public function change() {
        $goods_id = $this->request->param('goods_id', 0);
        $ope = $this->request->param('ope', 0); //操作 0-减 1-增
        if ($goods_id == 0) {
            $this->error('请选择商品');
        }


        Db::startTrans();
        try {


            $box_goods_info = db('user_box_goods')->where('user_box_id', $this->userBoxId)->where('goods_id', $goods_id)->find();
            $user_box_info = db('user_box')
                    ->where(['user_box_id' => $this->userBoxId, 'status' => 1])
                    ->find();
            $box_goods_id = 0;
            if ($box_goods_info) {
                $box_goods_id = $box_goods_info['box_goods_id'];
                $house_goods_info = db('house_goods')
                        ->alias('hg')
                        ->where(['hg.house_id' => $user_box_info['quarters_id'], 'hg.goods_id' => $goods_id, 'hg.is_del' => 0])
                        ->find();

                db('user_box_goods')->where('box_goods_id', $box_goods_info['box_goods_id'])->update(['selling_price' => $house_goods_info['selling_price']]);
            } else {

                $house_goods_info = db('house_goods')
                        ->alias('hg')
                        ->where(['hg.house_id' => $user_box_info['quarters_id'], 'hg.goods_id' => $goods_id, 'hg.is_del' => 0])
                        ->find();

                $arrBoxGoods['goods_id'] = $goods_id;
                $arrBoxGoods['user_box_id'] = $this->userBoxId;
                $arrBoxGoods['selling_price'] = $house_goods_info['selling_price'];

                $box_goods_id = db('user_box_goods')->insertGetId($arrBoxGoods);
            }

            $where['box_goods_id'] = $box_goods_id;
            $where['user_id'] = $this->iUserId;
            $where['user_box_id'] = $this->userBoxId;
            $where['type'] = 0;

            $cart_info = db('cart')->where($where)->find();

            $code = 1;  //编码  1 正常加减 80删除一条购物车记录   81 新增一条购物车记录 
            //如果已经添加过
            if ($cart_info) {
                $cwhere['id'] = $cart_info['id'];

                //如果只剩最后一个商品
                if ($cart_info['buy_num'] == 1 && $ope == 0) {
                    db('cart')->where($cwhere)->delete();
                    $code = 80;
                }

                if ($ope == 1) {
                    $cdata['buy_num'] = $cart_info['buy_num'] + 1;
                } else {
                    $cdata['buy_num'] = $cart_info['buy_num'] - 1;
                }
                $cdata['updatetime'] = date('Y-m-d H:i:s', time());
                db('cart')->where($cwhere)->update($cdata);
            } else {
                //新增一条购物车
                $data['box_goods_id'] = $box_goods_id;
                $data['user_box_id'] = $this->userBoxId;
                $data['user_id'] = $this->iUserId;
                $data['updatetime'] = date('Y-m-d H:i:s', time());
                db('cart')->insert($data);
                $code = 81;
            }

            //返回购物车总商品数
            $cart = getCartTotalByUserId($this->iUserId, $this->userBoxId);
            Db::commit();
            $this->success('操作成功', $cart, $code);
        } catch (Exception $ex) {
            Db::rollback();
            $this->error('数据操作出错');
        }
    }

    //根据商品ID获取商品详情
    public function getGoodsDetailById() {
        $id = $this->request->request('id', 0);
        if ($id == 0) {
            $this->error('请选择商品');
        }

        $gwhere['g.is_deleted'] = 0;
        $gwhere['g.status'] = 1;
        $gwhere['g.goods_id'] = $id;

        $goods_info = db('goods')
                ->alias('g')
                ->field('g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_desc,g.goods_logo,g.goods_spec,g.market_price,g.selling_price')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where($gwhere)
                ->find();

        if (empty($goods_info)) {
            $this->error('没有找到商品信息');
        }

        $box_goods_info = db('user_box_goods')->where('user_box_id', $this->userBoxId)->where('goods_id', $id)->find();

        if ($box_goods_info) {
            $buy_num = db('cart')->where(['user_id' => $this->iUserId, 'type' => 0, 'box_goods_id' => $box_goods_info['box_goods_id']])->value('buy_num');

            $goods_info['buy_num'] = $buy_num ? $buy_num : 0;
        } else {
            $goods_info['buy_num'] = 0;
        }

        $this->success('获取成功', $goods_info);
    }

    public function comfirmOrder() {
        $score = input('score', 0);
        $coupon_id = input('id', 0);

        $list = db('cart')
                ->alias('c')
                ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_desc,g.goods_logo,g.goods_spec,g.market_price,ubg.selling_price')
                ->join('fa_user_box_goods ubg', 'ubg.box_goods_id = c.box_goods_id')
                ->join('fa_goods g', 'ubg.goods_id=g.goods_id')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where(['c.user_id' => $this->iUserId, 'c.user_box_id' => $this->userBoxId, 'c.type' => 0])
                ->order('c.updatetime desc')
                ->select();

        if ($list) {
            $i = 1;
            foreach ($list as &$val) {
                $val['hide'] = 0;
                if ($i > 3) {
                    $val['hide'] = 1;
                }
                $val['buy_num'] = getCardNumByGoods($this->iUserId, $val['box_goods_id']);
                $i++;
            }

            if ($i <= 4) {
                $this->assign('is_hide', 1);
            }
        }
        //购物车数量及价格
        $cart = getCartTotalByUserId($this->iUserId, $this->userBoxId);
        //获取banner列表
//        $banner_list = db('ad')
//                ->where(['postion_id' => 2, 'type' => 1])
//                ->select();

        $banner_list = db('ad')
                ->alias('a')
                ->join('fa_ad_item ai', 'a.ad_id = ai.ad_id AND ai.position = 2', 'inner')
                ->where(['a.status' => 1, 'type' => 1])
                ->select();

        $coupon_list = couponList($this->iUserId, $this->userBoxId);
        $coupon_num = count($coupon_list);
        $user_info = db('users')->where(['user_id' => $this->iUserId])->find();

        $jifen_config = db('configure')->where(['key' => 'jifen'])->value('desc');
        $jifen_config = json_decode($jifen_config, true);


        $par_value = 0; //优惠券抵扣金额
        if ($coupon_id != 0) {
            $coupon_info = db('user_coupon')
                    ->alias('uc')
                    ->field('uc.*,c.coupon_name,c.par_value,c.use_condition,c.range,c.relation_id')
                    ->join('fa_coupon c', 'uc.coupon_id = c.coupon_id')
                    ->where('uc.status', 0)
                    ->where('user_id', $this->iUserId)
                    ->where('user_coupon_id', $coupon_id)
                    ->order('user_coupon_id desc')
                    ->find();

            if ($coupon_info) {
                $par_value = $coupon_info['par_value'];
                if ($cart['total_price'] <= $coupon_info['par_value']) {
                    $cart['total_price'] = 0;
                } else {
                    $cart['total_price'] -= $coupon_info['par_value'];
                }

                $this->assign('coupon_info', $coupon_info);
            }
        }



        $dikou = 0;
        $is_score = 0;
        if ($cart['total_price'] > 0) {
            $dikou = $user_info['score'] / $jifen_config['dikou'];
            if ($cart['total_price'] < $dikou) {
                $dikou = $cart['total_price'];
            }
            if ($score) {
                $is_score = 1;
            }
        } else {
            $score = 0;
        }

        $user_info['dikou'] = $dikou;

        $this->assign('par_value', $par_value);
        $this->assign('coupon_id', $coupon_id);
        $this->assign('score', $score);
        $this->assign('is_score', $is_score);
        $this->assign('coupon_num', $coupon_num ? $coupon_num : 0);
        $this->assign('user_info', $user_info);
        $this->assign('banner_list', $banner_list);
        $this->assign('list', $list);
        $this->assign('cart', $cart);
        return $this->view->fetch();
    }

    public function selCoupon() {
        $score = input('score', 0);

        $list = couponList($this->iUserId, $this->userBoxId);

        $this->assign('list', $list);
        $this->assign('score', $score);
        return $this->view->fetch();
    }

}
